home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / eulisp / you-075a.lha / you-075a / interpret.h < prev    next >
C/C++ Source or Header  |  1992-11-15  |  6KB  |  247 lines

  1. /*
  2.  * Defines of bytecode junk
  3.  */
  4.  
  5. #ifndef _INTERPRET_H
  6. #define _INTERPRET_H
  7. /*****************************************/
  8. /* For debugging */
  9. #ifndef NODEBUG
  10. #define BC_BUG(x)    x
  11. #define BC_BUG_EXP(x)   x
  12. #else
  13. #define BC_BUG(x)
  14. #define BC_BUG_EXP(x)   0
  15. #endif
  16.  
  17. #ifndef NODEBUG
  18. #define VCHECK(x) \
  19.   (( (x)!=NULL && (((int) (x))&1==1)) \
  20.    ? CallError(sp,"Dumb value",nil,NONCONTINUABLE) \
  21.    : nil)
  22. #else 
  23. #define VCHECK(x) 0
  24. #endif
  25.  
  26.  
  27. #ifdef COUNT_BYTES
  28. #define BC_COUNTER(x) x
  29. #else 
  30. #define BC_COUNTER(x)
  31. #endif
  32.  
  33. #define BC_PRESWITCH()     \
  34. BC_BUG({            \
  35.   fprintf(stderr,"{Doing: [%x, %x, %d] %d}\n",pc,sp,(int) (sp-oldsp),*pc); \
  36.   oldsp=sp;    \
  37.   }) \
  38. BC_COUNTER(exec_counts[*pc]++);
  39.  
  40. /* Global reference */
  41. #define GLOB_REF(n,m)     \
  42.   vref(statics[n],m)
  43.  
  44.  
  45. /* Stack hacking */
  46.  
  47. #define NTH_REF(sp,n)   (*((sp)-(n)))
  48.  
  49. #define SET_NTH_REF(sp,n,v) (*((sp)-(n))=v)
  50.  
  51. #define PUSH_VAL(sp,val)    ((*(++sp)=val))
  52.  
  53. #define POP_VALS(sp,n)        ((sp) -= (n))
  54.  
  55. #define PEEK_VAL(sp)        (*(sp))
  56.  
  57. #define TOP_VAL(sp)        (*(sp--))
  58.  
  59. #define SHOVE_VAL(sp,val) ((*(sp))=val)
  60.  
  61. #define SET_STACK(sp,val)    (sp)=(val);
  62.  
  63. /* Environment hacking */
  64.  
  65. #define ENV_NTH(e,depth)        \
  66. counter=depth;                \
  67. while (counter)                \
  68. {                    \
  69.   e=vref(e,0);                \
  70.   counter--;                \
  71.   VCHECK(e); \
  72. }
  73.  
  74. #define ENV_REF(e,into,depth,dist)    \
  75. ENV_NTH(e,depth)            \
  76. into=vref(e,dist+1);
  77.  
  78. #define SET_ENV_REF(e,depth,dist,val)    \
  79. ENV_NTH(e,depth)            \
  80. vref(e,dist+1)=val;
  81.  
  82. #define MAKE_ENV(sp,size)        \
  83. {                    \
  84.   LispObject tmp;            \
  85. /**/                    \
  86.   tmp=allocate_vector(sp+1, size+1);    \
  87.   vref(tmp,0)= PEEK_VAL(sp);        \
  88.   SHOVE_VAL(sp,tmp);            \
  89. }
  90.  
  91. /******************************/
  92. /* instruction stream hacking */
  93.  
  94. typedef unsigned char bytecode;
  95.  
  96. /* shoves arg into 'into' and updates pc */
  97. /* Should be a bit (read lots) cleverer  */
  98. #define read_int_arg(into,stream)    \
  99.   into= (int)(*(stream++));        \
  100.   into=(into<<8)+((int)(*(stream++)));        \
  101.   into=(into<<8)+((int)(*(stream++)));        \
  102.   into= *(stream++) ? -into: into;        \
  103.   BC_BUG(fprintf(stderr,"Read int: got: %d [%x]\n", into,into));
  104.  
  105. #define read_short_arg(into,stream) /* NOT YET */    \
  106.   into=1; stream+=2;
  107.  
  108. #define read_sign_arg(into,stream)    \
  109.   into=(int)((char) *(stream++));
  110.  
  111. #define read_byte_arg(into,stream) \
  112.   into = *(stream++);
  113.  
  114. #define skip_int_arg(pc)    pc+=sizeof(int)
  115.  
  116. #define next(stream)    stream++;
  117.  
  118. #define INC_PC(pc)    (pc++)
  119.  
  120. /* representation of BC on stack */
  121. #define PC_VAL_WIDTH 20
  122. #define PC_FLAG_WIDTH 2
  123. #define PC_FLAG       3
  124. #define PC_VECT_MASK  ((1<<(PC_VAL_WIDTH+PC_FLAG_WIDTH)) - 1)
  125.  
  126. #define bytevector_start(vector_number) (bytevectors[vector_number])
  127.  
  128. #define REIFY_PC(pc)                        \
  129. ((LispObject)                            \
  130.  ((this_vector<<(PC_VAL_WIDTH+PC_FLAG_WIDTH))            \
  131.   | (((pc)-bytevector_start(this_vector)) << PC_FLAG_WIDTH)     \
  132.   | PC_FLAG))
  133.  
  134. #define SET_PC(this_vector,reified_pc) \
  135. ((this_vector=((int)reified_pc)>>(PC_VAL_WIDTH+PC_FLAG_WIDTH)),    \
  136.  BC_BUG_EXP((this_vector==32 || this_vector==0) ? 0 : perror("wibble3")),\
  137.  reified_pc=((LispObject) (((int)(reified_pc))&PC_VECT_MASK)),    \
  138.  bytevector_start(this_vector)+((((int)reified_pc)>>PC_FLAG_WIDTH))        \
  139. )
  140.  
  141. /* modifies pc by x bytes */
  142. #define ADJUST_PC(pc,x)        \
  143.   ((pc)+((x)-1))
  144.  
  145. #define BF2PC(x) \
  146.   (this_vector=intval(bytefunction_codenum(x)),        \
  147.    BC_BUG_EXP(this_vector<=32 ? 0 : perror("wibble2")),        \
  148.    bytevector_start(intval(bytefunction_codenum(x)))    \
  149.    +intval(bytefunction_offset(x)))
  150.  
  151. /* Move sp to the start of a new nary list */
  152.  
  153.  
  154. /**********************/
  155. /* Garbage protection */
  156.  
  157. #define GC_RESTORE_GLOBALS        \
  158. {                \
  159.   if (1)            \
  160.     {                \
  161.       BCnil=nil;        \
  162.       BCtrue=lisptrue;        \
  163.     }                \
  164. }
  165.  
  166. /* Printing counts ... */
  167. #ifdef COUNT_BYTES
  168. #define PRINT_COUNTS    \
  169. {                \
  170.   int i,j;            \
  171.   for (i=0, j=0; i<256; i++)    \
  172.     {                \
  173.       if (exec_counts[i]!=0)    \
  174.     {                 \
  175.       fprintf(stderr,"%3d: %7d ",i,exec_counts[i]);     \
  176.       j++;            \
  177.       if ( (j%6) == 0)    \
  178.         fputc('\n',stderr);    \
  179.     }            \
  180.     }                \
  181.   if (j%6!=0) fputc('\n',stderr); \
  182. }      
  183. #else
  184. #define PRINT_COUNTS  fprintf(stderr,"Count-bytes: Couldn't tell you\n");
  185. #endif
  186. /*****************************************/
  187. /* Interpreter macros */
  188. #define MAX_MODS 256
  189.  
  190. #ifdef __STDC__
  191. #  ifndef NODEBUG
  192. #  define BC_CASE(name) \
  193.      case name: fprintf(stderr,"{Exec: "#name" [%x]}",(int)name,(int)pc); name##_CODE break;
  194. #  else
  195. #  define  BC_CASE(name)\
  196.      case name: name##_CODE break;
  197. #  endif
  198. #else /* stdc */
  199. #  ifndef NODEBUG
  200. #  define BC_CASE(name) \
  201. case name: fprintf(stderr,"{Exec: name [%x]}",(int)name,(int)pc); name/**/_CODE break;
  202.  
  203. #  else
  204. #  define BC_CASE(name) \
  205.  case name: name/**/_CODE  \
  206.   break
  207. #  endif
  208. #endif
  209.  
  210. #define N_GLOBALS 10
  211. #define GLOBAL_REF(n) vref(global_vector,(n))
  212. #define Generic_Lookup_Fn 0
  213. #define Generic_Apply_Fn 1
  214. #define Bci_Protect_Slot 2
  215.  
  216. #define BC_GLOBALS()    \
  217.   static LispObject boot_modules[MAX_BOOT_MODULES]; \
  218.   static int boot_module_count=1;               \
  219.   static bytecode exit_bytes[] = { BC_EXIT };        \
  220.   static SYSTEM_GLOBAL(int,static_count);    \
  221.   static LispObject *statics;    \
  222.   static LispObject static_vectors;    \
  223.   static LispObject global_vector;
  224.   /**/                    \
  225.   static bytecode **bytevectors;    \
  226.   BC_BUG(static LispObject *oldsp;)    \
  227.   BC_COUNTER(static int exec_counts[256];)    \
  228.  
  229. #define BC_INITIALISE_GLOBALS()        \
  230.   BCnil=nil;                \
  231.   BCtrue=lisptrue;            \
  232.   BC_BUG(oldsp=sp);            \
  233.   sp=stacktop-1;     /* stackpointer[0]= top elt */ \
  234.   pc=start_pc;            \
  235.   this_vector=context;        \
  236.  
  237. #define BC_NOINSTRUCT(pc)    \
  238.  default:            \
  239.   fprintf(stderr,"No such instruction: %d\n",pc);
  240.  
  241. /* GC Protection */
  242. #define SAVE_REGISTERS(sp)
  243.  
  244. #define RESTORE_REGISTERS(sp)
  245.  
  246. #endif _INTERPRET_H
  247.